<?php
/**
 * Description of Connection
 *
 * @author michamaciejewski
 */
class Connection {
  private $pdo = null;

  /**
   *
   * @param PDO $pdo
   */
  public function __construct(PDO $pdo) {
    $this->pdo = $pdo;
  }

  /**
   * @return mixed
   */
  public function query($queryString, $class, $parameters = null) {
    error_log($queryString);
    $stmt = $this->pdo->prepare($queryString);

    if (!$stmt) {
      throw new QueryException('Cannot prepare query: '. json_encode($this->pdo->errorInfo()));
    }

    if ($parameters) {
      foreach ($parameters as $parameterName => $value) {
        $stmt->bindValue(":$parameterName", $value);
      }
    }

    if (!$stmt->execute()) {
      throw new QueryException('Cannot execute query: '. json_encode($stmt->errorInfo()));
    }

    if ($class) {
      return $stmt->fetchAll(PDO::FETCH_CLASS, $class);
    } else {
      return $stmt->fetchAll(PDO::FETCH_OBJ, $class);
    }
  }

  /**
   *
   * @return PDO
   */
  public function getPdo() {
    return $this->pdo;
  }

  /**
   *
   * @param PDO $pdo
   */
  public function setPdo($pdo) {
    $this->pdo = $pdo;
  }
}
?>